<?php
namespace Admin\Controller;

class DepartmentController extends BaseController {

    public function index() {

        $Department = D ('Department');
        $departmentList = $Department->getAllDepartment();

        $this->assign('departmentList',$departmentList);
        $this->display();
    }



    public function add() {
        if (!IS_POST) {
            $this->display();
        } else {

            $data['name'] = $_REQUEST['name'];

            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $data['description'] = $_REQUEST['description'];

            $Department = D ('Department');
            $flag = $Department->addDepartment($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__APP__.'/Department');
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        }
    }


    public function edit() {

        if (!IS_POST) {

            $id = $_REQUEST['id'];
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
                $this->error(\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $Department = D ('Department');
            $d = $Department->getDepartmentById($id);
            if (empty($d)) $this->error('院系不存在');

            $this->assign('department',$d);
            $this->display();
        } else {

            $data['id'] = $_REQUEST['id'];
            $data['name'] = $_REQUEST['name'];
            $data['description'] = $_REQUEST['description'];

            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $Department = D ('Department');
            $flag = $Department->editDepartment($data);

            if ($flag === '000000')
                $this->out('RESULT_OK','',__APP__.'/Department');
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        }

    }


    public function del() {
        if (!IS_POST) $this->error('非法访问');

        $id = $_REQUEST['id'];
        if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        $Department = D ('Department');
        $flag = $Department->delDepartment($id);
        if ($flag === '000000')
            $this->out('RESULT_OK');
        $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

    }


}